var app = getApp();
var isclose = true;
var getList = function () {
  var _this = this;
  //调用应用实例的方法获取全局数据
  app.getUserInfo(function (userInfo) {
    wx.request({
      url: app.globalData.domain + 'api/wxapp_get?openId=' + userInfo.openId,
      success: function (res) {
        _this.setData({
          allActivity: res.data.project,
          time: (new Date()).Format('yyyy-MM-dd')
        });
        wx.hideNavigationBarLoading();
      }
    });
  });
};

Page({
    data: {

    },
    onLoad: function() {
        wx.setNavigationBarTitle({
            title: '我的项目'
        });
        getList.call(this);
    },
    onHide: function () {
      isclose = false;
    },
    onUnload: function () {
      isclose = true;
    },
    onShow: function () {
      wx.showNavigationBarLoading();
      if (!isclose) {
        getList.call(this);
      }
    },

    detail: function (e) {
          var id = e.target.dataset.id;
          if (!id) {
            return;
          }
          console.info(id);
          wx.navigateTo({
            url: '/pages/pjdetail/pjdetail?id=' + id
          });
    },
    delete: function (e) {
      var id = e.target.dataset.id;
      console.info(e);
      if (!id) {
        return;
      }
      console.info(id);
      var _this = this;
      wx.showModal({
        title: '删除数据',
        content: '删除后数据无法恢复，是否删除？',
        success: function (res) {
          if (res.confirm) {
            app.getUserInfo(function (userInfo) {
              wx.request({
                url: app.globalData.domain + 'api/wxapp_delete?projectid='+id+'&openId='+userInfo.openId,
                success: function (res) {
                  wx.showToast({
                    title: '删除成功',
                    icon: 'success',
                    duration: 2000
                  })
                  _this.setData({
                    allActivity: res.data.project,
                    time: (new Date()).Format('yyyy-MM-dd')
                  });
                }
              });
            })  

          }

        }
      })
      
    }

        
});

Date.prototype.Format = function (fmt) {
  var o = {
    "M+": this.getMonth() + 1, //月份 
    "d+": this.getDate(), //日 
    "h+": this.getHours(), //小时 
    "m+": this.getMinutes(), //分 
    "s+": this.getSeconds(), //秒 
    "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
    "S": this.getMilliseconds() //毫秒 
  };
  if (/(y+)/.test(fmt))
    fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  for (var k in o) {
    if (new RegExp("(" + k + ")").test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    }
  }
  return fmt;
}